﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;

namespace EIA.SLClient.ProjectManage.Basic
{
    public class ProjectInfoViewModel : SimpleMvvmToolkit.ViewModelDetailBase<ProjectInfoViewModel, EIA.PM.Model.TProjectBasic>
    {
        public ProjectInfoViewModel()
        {
            RegisterToReceiveMessages<EIA.PM.Model.TProjectBasic>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.PROJECT_DETAIL_SHOW, EP_Show);
            RegisterToReceiveMessages(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.PROJECT_DETAIL_HIDE, EP_Hide);
        }

        private void EP_Show(object sender, SimpleMvvmToolkit.NotificationEventArgs<EIA.PM.Model.TProjectBasic> e)
        {
            Model = e.Data;
        }
        private void EP_Hide(object sender, SimpleMvvmToolkit.NotificationEventArgs e)
        {
            if (IsEditing) this.CancelEdit();
            Model = null;
        }


        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }
        #endregion

        #region command

        #region SaveCommand

        private SimpleMvvmToolkit.DelegateCommand _SaveCommand = null;
        public SimpleMvvmToolkit.DelegateCommand SaveCommand
        {
            get
            {
                if (_SaveCommand == null)
                {
                    _SaveCommand = new SimpleMvvmToolkit.DelegateCommand(Save, CanExcuteSave);
                } return _SaveCommand;
            }
            set { _SaveCommand = value; NotifyPropertyChanged(m => m.SaveCommand); }
        }

        public void Save()
        {
            EIA.PM.BLL.ClientProxy.TProjectBasicProxy proxy = new PM.BLL.ClientProxy.TProjectBasicProxy();
            Busy();
            proxy.Update(Model, (r) =>
            {
                Free();
                if (r)
                {
                    EndEdit();
                }
                else
                {
                    System.Windows.MessageBox.Show("保存失败！", "提示", System.Windows.MessageBoxButton.OK);
                }
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("保存时发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteSave()
        {
            return true;
        }

        #endregion

        #region CancelCommand

        private SimpleMvvmToolkit.DelegateCommand _CancelCommand = null;
        public SimpleMvvmToolkit.DelegateCommand CancelCommand
        {
            get
            {
                if (_CancelCommand == null)
                {
                    _CancelCommand = new SimpleMvvmToolkit.DelegateCommand(Cancel, CanExcuteCancel);
                } return _CancelCommand;
            }
            set { _CancelCommand = value; NotifyPropertyChanged(m => m.CancelCommand); }
        }

        public void Cancel()
        {
            this.CancelEdit();
        }

        public bool CanExcuteCancel()
        {
            return true;
        }

        #endregion


        #region EditCommand

        private SimpleMvvmToolkit.DelegateCommand _EditCommand = null;
        public SimpleMvvmToolkit.DelegateCommand EditCommand
        {
            get
            {
                if (_EditCommand == null)
                {
                    _EditCommand = new SimpleMvvmToolkit.DelegateCommand(Edit, CanExcuteEdit);
                } return _EditCommand;
            }
            set { _EditCommand = value; NotifyPropertyChanged(m => m.EditCommand); }
        }

        public void Edit()
        {
            this.BeginEdit();
        }

        public bool CanExcuteEdit()
        {
            return true;
        }

        #endregion

        #endregion

        public void OnLoad()
        {
            this.SendMessage<object, EIA.PM.Model.TProjectBasic>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.PROJECT_CURRENT, new SimpleMvvmToolkit.NotificationEventArgs<object, PM.Model.TProjectBasic>("", null, (p) =>
            {
                if (Model != p) Model = p;
            }));
        }
    }
}